﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ATW.MT.BUS;
using System.Data;

namespace ATW.MT.SellManage
{
    public partial class _addTemp : System.Web.UI.Page
    {
        bus_C_Temp temp = new bus_C_Temp();
        bus_D_SystemArgs ds = new bus_D_SystemArgs();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
            {
                string tempid = Request.QueryString["pid"];
                if (!CommonClass.common.filterChar(tempid))
                {
                    return;
                }
                try
                {
                    DataTable dt = temp.TempSingleQuery(tempid);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        txtDesc.Text = dt.Rows[0]["TDesc"].ToString();
                        txtUp.Text = dt.Rows[0]["UP"].ToString();
                        txtDown.Text = dt.Rows[0]["down"].ToString();
                        txtRate.Text = dt.Rows[0]["rate"].ToString();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
        protected void queryButton_Click(object sender, EventArgs e)
        {
            string desc = txtDesc.Text.Trim();
            string up = txtUp.Text.Trim();
            string down = txtDown.Text.Trim();
            string rate = txtRate.Text.Trim();
            decimal _up = 0;
            decimal _down = 0;
            decimal _rate = 1;
            decimal _limitMax = decimal.Parse(ds.GetSysArgs("MaxRate"));
            decimal _limitMin = decimal.Parse(ds.GetSysArgs("MinRate"));
            try
            {
                #region 校验
                if (desc == "")
                {
                    Tips("请输入区间描述！");
                    return;
                }
                if (up == "")
                {
                    Tips("请输入区间上限！");
                    return;
                }
                if (down == "")
                {
                    Tips("请输入区间下限！");
                    return;
                }
                if (rate == "")
                {
                    Tips("请输入区间价格系数！");
                    return;
                }
                if (!CommonClass.common.filterChar(desc))
                {
                    Tips("区间描述中存在非法字符！");
                    return;
                }
                if (!decimal.TryParse(up, out _up))
                {
                    Tips("区间上限应当为数字！");
                    return;
                }
                if (!decimal.TryParse(down, out _down))
                {
                    Tips("区间下限应当为数字！");
                    return;
                }
                if (_up < _down)
                {
                    Tips("区间上限应当大于区间下限！");
                    return;
                }
                if (!decimal.TryParse(rate, out _rate) || _rate <= 0)
                {
                    Tips("区间价格系数应当为正整数或小数");
                    return;
                }
                if (_rate < _limitMin || _rate > _limitMax)
                {
                    Tips("价格系数超过可调范围限制！");
                    return;
                }
                #endregion
                if (string.IsNullOrEmpty(Request.QueryString["pid"]))
                {
                    int result = temp.TempAdd(desc, up, down, rate);
                    if (result > 0)
                    {
                        Tips("新增成功!");
                        CloseWindowRefresh();
                    }
                    else
                    {
                        Tips("新增失败！");
                    }
                }
                else
                {
                    string tempID = Request.QueryString["pid"];
                    int result = temp.TempEdit(desc, up, down, rate, tempID);
                    if (result > 0)
                    {
                        Tips("修改成功！");
                        CloseWindowRefresh();
                    }
                    else
                    {
                        Tips("修改失败！");
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }
        private void CloseWindowRefresh()
        {
            string strScript = "<script language=javascript>";
            strScript += "if(window.opener){";
            strScript += "window.opener.location.href=window.opener.location.href";
            strScript += "};";
            strScript += "window.close()";
            strScript += "</script>";
            this.ClientScript.RegisterStartupScript(this.GetType(), "closeWin", strScript.ToString());
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }//提示
    }
}